﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace DevExpressDemos.Controls.GridControl
{
    public partial class 显示行号 : Frm_GridControlDemo
    {
        public 显示行号()
        {
            InitializeComponent();
            BindCustomDrawRowIndicator(gridView1);
        }
        /// <summary>
        /// 绑定自定义行号事件
        /// </summary>
        /// <param name="sender"></param>
        private void BindCustomDrawRowIndicator(object sender)
        {
            GridView view = sender as GridView;
            if (view == null)
                return;
            view.IndicatorWidth = 30;
            view.TopRowChanged += view_TopRowChanged;
            view.CustomDrawRowIndicator += view_CustomDrawRowIndicator;
        }
        /// <summary>
        /// 根据滚轮位置设置行头宽度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void view_TopRowChanged(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            if (view == null)
                return;
            int width = CalcIndicatorBestWidth(view);//10大概为选择图标的宽度
            if ((view.IndicatorWidth - 4 < width || view.IndicatorWidth + 4 > width) && view.IndicatorWidth != width)
            {
                view.IndicatorWidth = width;
            }
        }
        /// <summary>
        /// 绘制行头事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void view_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            var view = sender as GridView;
            if (view == null)
                return;
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
            else if (e.Info.IsRowIndicator && view.IsGroupRow(e.RowHandle))
            {
                e.Info.DisplayText = (-e.RowHandle).ToString();
                e.Appearance.ForeColor = view.Appearance.GroupRow.ForeColor;
                e.Appearance.Font = view.Appearance.GroupRow.Font;
            }
        }
        /// <summary>
        /// 计算行头宽度
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        private int CalcIndicatorBestWidth(object sender)
        {
            Graphics graphics = new Control().CreateGraphics();
            SizeF sizeF = new SizeF();
            if (sender is GridView)
            {
                var view = sender as GridView;
                int count = view.TopRowIndex + ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)view.GetViewInfo()).RowsInfo.Count;
                sizeF = graphics.MeasureString(count.ToString(), view.Appearance.Row.Font);
            }
            return Convert.ToInt32(sizeF.Width) + 20;
        }
    }
}